/*
AlliedBridge Content Management System
Copyright (C) 2006 AlliedBridge

This program is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software Foundation;

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.volume4.alliedbridge.tools;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AddJoiningPoints extends HttpServlet {

	// Should the following three String declarations be private or final?
	// Should they be in an interface?
	public String DRIVER, URL, USER, PASS;

	public String messageSuccess, messageFailed, app_root, jp;

	public void init() throws ServletException {
		ServletContext context = getServletContext();
		DRIVER = context.getInitParameter("driver");
		URL = context.getInitParameter("mc_dburl");
		USER = context.getInitParameter("mc_dbuser");
		PASS = context.getInitParameter("mc_dbpass");
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		Connection con = null;
		// These need to be moved to a properties file.
		messageSuccess = "Joining points successfully added to database.";
		// For failed messages more information should be provided.
		// For example a formatted stackTrace() or system message.
		messageFailed = "Operation failed.";

		try {
			Class.forName(DRIVER);
			con = DriverManager.getConnection(URL, USER, PASS);

			jp = request.getParameter("jp");
			System.out.println(jp);
			String query = "UPDATE ab_leader_board SET jp= '" + jp + "'";

			PreparedStatement stmt = con.prepareStatement(query);
			int result = stmt.executeUpdate(query);

			app_root = request.getContextPath();
			response.sendRedirect(app_root
							+ "/application/application_messages/process_result.jsp?message="
							+ messageSuccess);

		} catch (SQLException ex) {
			System.out.println("\nERROR:------ SQLException -----\n");
			while (ex != null) {
				System.out.println("Message: " + ex.getMessage());
				System.out.println("SQLState: " + ex.getSQLState());
				System.out.println("ErrorCode :" + ex.getErrorCode());
				ex = ex.getNextException();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException ex) {
				System.out.println("\nERROR:------ SQLException -----\n");
				System.out.println("Message: " + ex.getMessage());
				System.out.println("SQLState: " + ex.getSQLState());
				System.out.println("ErrorCode :" + ex.getErrorCode());
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}